package com.ocl.www.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.web.ServletContextApplicationContextInitializer;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.ServletConfigAware;

import com.ocl.www.basic.BasicController;
import com.ocl.www.domain.UserAttention;
import com.ocl.www.service.UserAttentionService;


@RestController
@RequestMapping("/userAttention")
public class UserAttentionController extends BasicController {

	@Autowired
	public UserAttentionService userAttentionService;
	
	@Autowired
	private  HttpServletRequest request ;
	
	
	@Autowired
	private EhCacheManager em;


    @ResponseBody
    @RequestMapping(value="/queryAttentionOfUser",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<UserAttention> queryAttentionOfUser(@RequestBody Integer userId) {

        List<UserAttention> userAttentionList = null;
        try {

            userAttentionList = userAttentionService.queryAttentionOfUser(userId);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return userAttentionList;

    }


        /**
         *
         * @Discription 添加用户兴趣爱好
         * @param categotyId
         * @param userId
         * @return
         */
	@ResponseBody
	@RequestMapping(value="/insertUserAttention",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public int insertUserAttention(@RequestBody List<Map<String,String>> list){
		
		
		///System.out.println(ss);
		List<UserAttention> userAttentionlist = new ArrayList<UserAttention>();

		for(int i = 0 ;i<list.size();i++){
			
			int cid = Integer.parseInt(list.get(i).get("categoryId"));
			String cname = list.get(i).get("categoryName");
			
			int userId = (int)em.getCache("shiroCache").get("user_id");
			System.out.println("userid:"+userId);
			UserAttention userAttention = new UserAttention();
			
			userAttention.setCategoryId(cid);
			
			userAttention.setCategoryName(cname);
			
			userAttention.setUserId(userId);
			
			Date d = new Date();
			
			userAttention.setCreateTime(new Timestamp(d.getTime()));
			
			userAttentionlist.add(userAttention);
			
		}
		int count = 0 ;

		if (userAttentionlist.isEmpty()){
			return count;

		}

		try {
			count  = userAttentionService.insertUserAttention(userAttentionlist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return count;
	}
}
